/**
 * 
 */
package vn.smartware.co.framework.swt.base;

import java.util.ArrayList;

import org.eclipse.swt.custom.CTabFolder;
import org.eclipse.swt.custom.CTabItem;

import vn.smartware.co.framework.bean.ProgramDTO;
import vn.smartware.co.framework.exception.FatalException;


/**
 * @author thuctt
 *
 */
@SuppressWarnings("rawtypes")
public class SWTManager {
	
	private static SWTManager swtManager;
	
	/**
	 * @return SWTManager
	 */
	public static SWTManager getInstance(){
		
		if (swtManager == null){
			swtManager = new SWTManager();
		}
		return swtManager;
	}

	private SWTBase baseManager;
	
	private CTabFolder tabFolder;
	
	private ArrayList prgList;
	
	/**
	 * @return the prgList
	 */
	public ArrayList getPrgList() {
		return prgList;
	}

	/**
	 * @param prgList the prgList to set
	 */
	public void setPrgList(ArrayList prgList) {
		this.prgList = prgList;
	}

	/**
	 * @return CTabItem
	 */
	public CTabItem getSelectingTabItem(){
		
		return tabFolder.getSelection();
	}
	
	/**
	 * @return int
	 */
	public int getSelectingTabIndex(){
		
		return tabFolder.getSelectionIndex();
	}

	/**
	 * @return the tabFolder
	 */
	public CTabFolder getTabFolder() {
		return tabFolder;
	}

	/**
	 * @param tabFolder the tabFolder to set
	 */
	public void setTabFolder(CTabFolder tabFolder) {
		this.tabFolder = tabFolder;
	}

	/**
	 * @return the baseManager
	 */
	public SWTBase getBaseManager() {
		return baseManager;
	}

	/**
	 * @param baseManager the baseManager to set
	 */
	public void setBaseManager(SWTBase baseManager) {
		this.baseManager = baseManager;
	}
	
	/**
	 * start a business
	 * @param clazz
	 * @throws FatalException
	 */
	public static void businessStart(Class clazz) throws FatalException {
		
		SWTManager bussinessManager = SWTManager.getInstance();
		SWTBase scrBase = bussinessManager.getBaseManager();
		ProgramDTO prg = lookupPrgList(clazz.getName(), bussinessManager.getPrgList());
		if (null == prg){
			throw new FatalException("Business invalid.");
		}
		scrBase.businessStart(prg);
	}

	/**
	 * search this business in program list 
	 * @param className
	 * @param list
	 * @return ProgramDTO
	 */
	private static ProgramDTO lookupPrgList(String className, ArrayList list) {
		
		for (int i = 0; i < list.size(); i++) {
			ProgramDTO prg = (ProgramDTO)list.get(i);
			if (className.indexOf(prg.getPrgClass()) != -1){
				return prg;
			}
		}
		return null;
	}
}
